//
//  MBCommodityBundledCell.swift
//  iPhone
//
//  Created by Toj on 8/15/22.
//
//  商品规格 -> 捆绑包

import UIKit

protocol MBCommodityBundledCellDelegate: NSObjectProtocol {
    
//    func commodityBundledCellDidTapBuy(_ view: MBCommodityBundledCell)
    
    func commodityBundledCellDidSelected(_ view: MBCommodityBundledCell)
}

// MARK: - 懒加载(属性), 变量, 初始化init
class MBCommodityBundledCell: UICollectionViewCell {
    weak var delegate: MBCommodityBundledCellDelegate?
    
    var model: MemberCommodityDto? { didSet {
        guard let commodity = model else { return }
        
        unitPriceView.setTitle(commodity.detail_info, for: .normal)
        backImageView.isSelected = commodity.isSelected
        
        // 商品吊牌价
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .byTruncatingTail
        paragraphStyle.alignment = .center
        let symbol = commodity.currency_symbol
        let price = commodity.price
        let font = UIFont.bold(with: 10.f_scale)
        let symbolAttributed = symbol.to_attributedString(font: font, color: priceLabel.textColor, paragraph: paragraphStyle)
        let priceAttributed = price.st_moneyFormat.st_cleanZero.to_attributedString(font: UIFont.boldSystemFont(ofSize: 21.f_scale), color: priceLabel.textColor, paragraph: paragraphStyle)
        symbolAttributed.append(priceAttributed)
        priceLabel.attributedText = symbolAttributed
        
        // 商品折扣价
        let list_price = commodity.list_price
        let list_priceString = symbol + list_price.st_moneyFormat.st_cleanZero
        let invalidPriceAttributed = list_priceString.to_attributedString(font: invalidPriceLabel.font, color: invalidPriceLabel.textColor, paragraph: paragraphStyle, lineStyle: .strikethrough)
        invalidPriceLabel.attributedText = invalidPriceAttributed
        invalidPriceLabel.isHidden = list_price <= price
        if(!commodity.isShowed) {
            commodity.isShowed = true
            let ext = [DSKExtension.channel_info.rawValue: commodity.channelInfo]
            let event = DSEvent(category: st_className, action: "ProdShow", label: commodity.name, value: "1")
            DataStatistics.sendEvent(event: event, msg: ext)
        }
        
        setupMakeRelayoutSubviews()
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer.init(target:self, action:#selector(didTapBundled))
        addGestureRecognizer(tapGesture)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    // 选择边框View
    private lazy var backImageView: UIButton = {
        let view = UIButton()
        view.isUserInteractionEnabled = false
        view.setBackgroundImage("commodity_border_normal".to_image, for: .normal)
        view.setBackgroundImage("commodity_border_sel".to_image, for: .selected)
        return view
    }()
    
    // 时间背景图
    private lazy var unitPriceView: UIButton = {
        let view = UIButton()
        view.titleLabel?.numberOfLines = 1
        view.titleLabel?.textAlignment = .center
        view.isUserInteractionEnabled = false
        view.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16.f_scale)
        view.setTitleColor("#775E33".to_color, for: .normal)
        view.setBackgroundImage("commodity_mb_bundled_bg_normal".to_image, for: .normal)
        view.setBackgroundImage("commodity_mb_bundled_bg_sel".to_image, for: .selected)
        return view
    }()
    
    // 现价格
    private lazy var priceLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#554422".to_color
        view.textAlignment = .center
        return view
    }()
    
    // 原价
    private lazy var invalidPriceLabel: UILabel = {
        let view = UILabel()
        view.textAlignment = .center
        view.textColor = "#AAAAAA".to_color
        view.font = UIFont.bold(with: 14.f_scale)
        return view
    }()
    
//    // 购买按钮
//    private lazy var buyLabel: UILabel = {
//       let view = UILabel()
//        view.textAlignment = .center
//        view.text = "buy".localized()
//        view.font = UIFont.boldSystemFont(ofSize: 14.f_scale)
//        view.textColor = "#775E33".to_color
//        return view
//    }()
//    private let buyButtonHeight: CGFloat = 40.ui_scale
//    private lazy var buyButton: UIButton = {
//        let view = UIButton()
//        view.st_cornerRadius = buyButtonHeight * 0.5
//        view.backgroundColor = "#E8CD8C".to_color
//        view.addTarget(self, action: #selector(didTapBuy), for: .touchUpInside)
//        return view
//    }()
}

// MARK: - 点击事件
extension MBCommodityBundledCell {
    
    @objc private func didTapBundled() {
        delegate?.commodityBundledCellDidSelected(self)
    }
    
    @objc private func didTapBuy() {
//        delegate?.commodityBundledCellDidTapBuy(self)
    }
}

// MARK: - 初始化
extension MBCommodityBundledCell {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(unitPriceView)
        contentView.addSubview(backImageView)
        contentView.addSubview(priceLabel)
        contentView.addSubview(invalidPriceLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        
        backImageView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        unitPriceView.snp.makeConstraints { make in
            make.top.bottom.left.equalTo(contentView)
            make.width.equalTo(150.ui_scale)
        }
        
        setupMakeRelayoutSubviews()
    }
    
    private func setupMakeRelayoutSubviews() {
        if !invalidPriceLabel.isHidden {
            invalidPriceLabel.snp.remakeConstraints { make in
                make.left.equalTo(unitPriceView.snp.right)
                make.right.equalTo(contentView)
                make.bottom.equalTo(contentView.snp.bottom).offset(-12)
            }
            
            priceLabel.snp.remakeConstraints { make in
                make.width.left.equalTo(invalidPriceLabel)
                make.bottom.equalTo(invalidPriceLabel.snp.top)
            }
            return
        }
        
        priceLabel.snp.remakeConstraints { make in
            make.left.equalTo(unitPriceView.snp.right)
            make.centerY.right.equalTo(contentView)
        }
    }
}
